package com.hua.api.modules.finance.service.impl;

import com.alipay.api.domain.Bill;
import com.hua.api.core.constants.Status;
import com.hua.api.core.framework.util.DateUtil;
import com.hua.api.modules.finance.domain.OrderBill;
import com.hua.api.modules.finance.repository.OrderBillRepository;
import com.hua.api.modules.finance.service.OrderBillService;
import com.hua.api.modules.finance.service.UserAccountService;
import com.hua.api.modules.order.dao.OrderInfoDao;
import com.hua.api.modules.order.dao.ShopInfoDao;
import com.hua.api.modules.order.domain.OrderInfo;
import com.hua.api.modules.order.domain.ShopInfo;
import com.hua.api.modules.order.service.OrderInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class OrderBillServiceImpl implements OrderBillService {

    private final ShopInfoDao shopInfoDao;

    private final OrderInfoDao orderInfoDao;

    private final OrderBillRepository orderBillRepository;

    private final UserAccountService userAccountService;


    /**
     * 订单结算
     * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void orderBillJob() {
        log.info("OrderBillServiceImpl.orderBillJob 订单结算开始");
        Date endDate = DateUtil.offsetHour(new Date(),-1); // 一小时之前的订单结算
        List<OrderInfo> list = orderInfoDao.queryNeedBillOrderInfo(endDate);
        log.info("OrderBillServiceImpl.orderBillJob 结算数量={}",list.size());
        for( OrderInfo orderInfo:list ){
            log.info("OrderBillServiceImpl.orderBillJob 结算订单={}",orderInfo.getOrderNo());
            OrderBill orderBill = orderBillRepository.findOrderBillByOrderNo(orderInfo.getOrderNo());
            if(null==orderBill){
                OrderBill bill = addOrderBill(orderInfo);
                if(null!=bill){
                    // 入账
                    userAccountService.onUserAccountAdd(bill.getUserId(),bill.getRealMoneyYuan(),"order_bill","订单结算入账金额,单号["+orderInfo.getId()+"]");
                    log.info("OrderBillServiceImpl.orderBillJob 结算订单结算完成={}",orderInfo.getOrderNo());

                    // 标记为已结算
                    orderInfo.setBill(Status.BillMode.bill_yes.getValue());
                    orderInfo.setUtime(new Date());
                    orderInfoDao.updateObject(orderInfo);

                }
            }else {
                // 标记为已结算
                orderInfo.setBill(Status.BillMode.bill_yes.getValue());
                orderInfo.setUtime(new Date());
                orderInfoDao.updateObject(orderInfo);
                log.warn("OrderBillServiceImpl.orderBillJob 订单号={},已经结算。无需重新结算！",orderInfo.getOrderNo());
            }
        }
    }

//    /**
//     * 已配送完成的订单，重置为完成。
//     * */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public void orderDoneJob() {
//        Date endDate = DateUtil.offsetDate(new Date(),-7); // 7天前
//        log.info("OrderBillServiceImpl.orderDoneJob 订单完成任务 endDate={}",DateUtil.getFormatDate(endDate,"yyyy-MM-dd HH:mm:ss"));
//        List<OrderInfo> list = orderInfoDao.queryNeedSetToDoneOrderInfo(endDate);
//        log.info("OrderBillServiceImpl.orderDoneJob 订单完成任务 endDate={},size={}",DateUtil.getFormatDate(endDate,"yyyy-MM-dd HH:mm:ss"),list.size());
//        for( OrderInfo orderInfo:list ){
//            orderInfoService.onOrderInfoFinish(orderInfo.getOrderNo());
//        }
//        log.info("OrderBillServiceImpl.orderDoneJob 任务完成！");
//    }



    @Transactional(rollbackFor = Exception.class)
    public OrderBill addOrderBill(OrderInfo orderInfo){
        ShopInfo shopInfo = shopInfoDao.getObject(ShopInfo.class,orderInfo.getShopId());
        if(null!=shopInfo){
            OrderBill bill = new OrderBill();
            bill.setBillDesc("订单结算！");
            bill.setOrderNo(orderInfo.getOrderNo());
            bill.setCreatedTime(new Date());
            bill.setTotalMoneyYuan(orderInfo.getTotal());
            bill.setRealMoneyYuan(orderInfo.getRealMoneyYuan());
            bill.setProfitMoneyYuan(orderInfo.getTotal()-orderInfo.getRealMoneyYuan());
            bill.setState(Status.LOGIC_Y);
            bill.setUserId(shopInfo.getUserId());
            bill.setShopId(shopInfo.getId());
            orderBillRepository.save(bill);
            return bill;
        }else {
            log.warn("订单店铺不存在，无法结算！ shopId={}, orderNo={}",orderInfo.getShopId(),orderInfo.getOrderNo());
        }
        return null;
    }




}